package edu.polsl.bio.tools.str;

import edu.polsl.bio.tools.alignment.LightWeightAligner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

/**
 * //todo class description
 * <p/>
 * Created on: Aug 31, 2010 1:37:54 PM <br/>
 * <a href="http://www.polsl.pl">www.polsl.pl</a>
 *
 * @author lukasz.olczak@polsl.pl
 */
public class StrMatcher  implements Serializable {
  /**
   * slf4j Logger.
   */
  private static final Logger log = LoggerFactory.getLogger(StrMatcher.class.getName());

  private LightWeightAligner lAligner;

  private LightWeightAligner rAligner;

  private boolean useLeftFlank;

  private boolean useRightFlank;

  private int cutoff;

  public StrMatcher(int lSize, int rSize, boolean useLeftFlank, boolean useRightFlank, int match, int mismatch, int indel, int cutoff) {
    lAligner = new LightWeightAligner(lSize, match, mismatch, indel);
    rAligner = new LightWeightAligner(rSize, match, mismatch, indel);
    this.useLeftFlank = useLeftFlank;
    this.useRightFlank = useRightFlank;
    this.cutoff = cutoff;
  }

  public boolean match(StrRecord str1, StrRecord str2) {
    if (!str1.getMotif().equals(str2.getMotif())) {
      return false;
    }
    if (useLeftFlank) {
      int score = lAligner.align(str1.getLeftFlank(), str2.getLeftFlank());
      if (score < cutoff) {
        return false;
      }
    }
    if (useRightFlank) {
      int score = rAligner.align(str1.getRightFlank(), str2.getRightFlank());
      if (score < cutoff) {
        return false;
      }
    }

    return true;
  }
}

